package br.com.hotel68.view.text;

import static java.lang.System.out;

import java.util.Collection;
import java.util.Scanner;

import br.com.hotel68.Hotel68BusinessFacade;
import br.com.hotel68.model.Client;
import br.com.hotel68.model.Room;
import br.com.hotel68.model.RoomType;
import br.com.hotel68.view.UserInterface;

public class TextUserInterface implements UserInterface {

	private Hotel68BusinessFacade facade;

	public TextUserInterface(Hotel68BusinessFacade businessFacade) {
		this.facade = businessFacade;
	}

	@Override
	public void showInterface() {

		while (true) {

			this.showMenu();

			int operation = this.getSelectedOperation();

			switch (operation) {
			case 1:
				this.listClients();
				this.waitForUser();
				break;
			case 2:
				this.listRooms();
				this.waitForUser();
				break;
			case 3:
				this.listRoomTypes();
				this.waitForUser();
				break;
			case 0:
				System.out.println("Saindo...");
				return;
			default:
				System.out.println("Opcao invalida");
			}
		}
	}

	/**
	 * Mostra o menu para o usuario.
	 */
	private void showMenu() {
		out.println("");
		out.println("------------------------------------");
		out.println("           Hotel 68");
		out.println("");
		out.println("                       versao " + facade.version());
		out.println("------------------------------------");
		out.println("");
		out.println("Menu principal");
		out.println("");
		out.println("1. Listar clientes");
		out.println("2. Listar quartos");
		out.println("3. Listar tipos de quartos");
		out.println("");
		out.println("0. Sair");
	}

	/**
	 * Obtem a operacao selecionada pelo usuario.
	 */
	@SuppressWarnings("resource")
	private int getSelectedOperation() {
		System.out.println("");
		System.out.println("Digite uma operação: ");
		return new Scanner(System.in).nextInt();
	}

	/**
	 * Soh espera o usuario visualizar o resultado de uma operacao.
	 */
	@SuppressWarnings("resource")
	private void waitForUser() {
		System.out.println("");
		System.out.println("Tecle <enter> para continuar...");
		new Scanner(System.in).nextLine();
	}

	/**
	 * Metodo responsavel por apresentar ao usuario a lista de quartos.
	 */
	private void listRooms() {
		Collection<Room> rooms = this.facade.findRooms();
		for (Room r : rooms) {
			System.out.printf("Quarto %s, tipo %s%n", r.getName(), r.getType());
		}
	}

	private void listRoomTypes() {
		Collection<RoomType> types = this.facade.findRoomTypes();
		for (RoomType type : types) {
			System.out.printf("Tipo de quarto %s: %s, Valor: R$ %.2f%n", type, type.getDescription(), type.getValue());
		}
	}

	/**
	 * Metodo responsavel por apresentar ao usuario a lista de clientes.
	 */
	private void listClients() {
		Collection<Client> clients = this.facade.findClients();
		for (Client c : clients) {
			System.out.printf("Cliente: %s%n", c.getName());
		}
		
		//Client client = this.facade.findClientById(2);
		//System.out.printf("Cliente ESPECIFICO: %s%n", client.getName());
	}
}
